package concurrency.reentrantLock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;
@Slf4j
public class ReentrantLockTest2_Interrupt {

    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        //可打断
        Thread t1 = new Thread(() -> {
            try {
                log.debug("尝试获得锁。。");
                lock.lockInterruptibly();
            } catch (InterruptedException e) {
                log.error("中断异常:",e);
                log.error("锁被中断了，不等了，返回");
                return;
            }
            try{
                log.debug("拿到锁了");
            }finally{
                lock.unlock();
            }
        }, "reentrantlock可打断测试线程");

        lock.lock();
        log.debug("获取到锁了");
        t1.start();
        try {
            Thread.sleep(1000);
            t1.interrupt();
            log.debug("执行中断");
        } catch(InterruptedException e){
            e.printStackTrace();
        } finally
        {
            lock.unlock();
        }
    }


}
